package com.yundou.www.wrpc.core.net;

import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Objects;

/**
 * @author wangweiwei
 * @since 2021/7/20
 */
public class ServerBusinessHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

//        System.out.println(s);
        WRpcRequest request = JSON.parseObject(s ,WRpcRequest.class);

        WRpcResponse response = null;
        if (Objects.nonNull(request.getCheckSession()) && request.getCheckSession() == 1) {
            response = new WRpcResponse();
            response.setCode(9999);

        } else {
            response = ServiceProxy.doExecuteRequest(request);
        }
        response.setId(request.getId());
        String s1 = JSON.toJSONString(response)+"$";
//        System.out.println("响应   " + s1);
        channelHandlerContext.writeAndFlush(s1);

    }
}
